﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace NewsSpider.App
{
    public partial class LoginFrm : Form
    {
        NewsSpider.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
        NewsSpider.Utility.IniFile iniFile2 = new Utility.IniFile(Application.StartupPath + "\\version.ini");

        public LoginFrm()
        {
            InitializeComponent();
        }

        private void LoginFrm_Load(object sender, EventArgs e)
        {
            try
            {
                skinEngine1.SkinFile = NewsSpider.Utility.CommonHelper.GetCurrentSoftSkin();
                this.Icon = new Icon(NewsSpider.Utility.ICONHelper.SetFormICON());

                string strGetRemember = iniFile.GetString("Users", "remember", "");
                if (strGetRemember == "1")
                {
                    cbRemember.Checked = true;
                    txtUserName.Text = iniFile.GetString("Users", "name", "");
                    txtPassword.Text = iniFile.GetString("Users", "password", "");
                }
                else
                {
                    cbRemember.Checked = false;
                    txtUserName.Text = "";
                    txtPassword.Text = "";
                }

                string strGetVersion = iniFile2.GetString("cloudin_newsspider", "version", "");
                this.Text = "云里新闻采集大师 " + strGetVersion + "(正在更新...)";
            }
            catch (Exception ex)
            {
                NewsSpider.Utility.Log.SaveErrorLog("用户登录初始化 -> " + ex.Message);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            CheckLogin();
        }

        /// <summary>
        /// 检测登陆
        /// </summary>
        private void CheckLogin()
        {
            try
            {
                string strUserName = txtUserName.Text.Trim();
                string strPassword = txtPassword.Text.Trim();

                if (string.IsNullOrEmpty(strUserName))
                {
                    MessageBox.Show("请输入您的账号！");
                    txtUserName.Focus();
                }
                else if (string.IsNullOrEmpty(strPassword))
                {
                    MessageBox.Show("请输入您的密码！");
                    txtPassword.Focus();
                }
                else
                {

                    //验证账号
                    SqlConnection cn = new SqlConnection(NewsSpider.DBSqlHelp.DBSQLiteHelper.ConnectionCloudin());
                    cn.Open();
                    string strSql = "select user_id,user_password,real_name from Cloudin_Users where user_name='" + strUserName + "'";
                    SqlCommand cm = new SqlCommand(strSql, cn);
                    SqlDataReader dr = cm.ExecuteReader();
                    if (dr.Read())
                    {
                        string strOldPassword = dr["user_password"].ToString().Trim();
                        string strUserID = dr["user_id"].ToString().Trim();
                        string strRealName = dr["real_name"].ToString().Trim();

                        if (strOldPassword == strPassword)
                        {
                            iniFile.WriteValue("INFO", "userid", strUserID);
                            iniFile.WriteValue("INFO", "customer", strRealName);
                            btnLogin.Enabled = false;
                            btnLogin.Text = "正在登陆";

                            //判断是否记录账号及密码
                            if (cbRemember.Checked)
                            {
                                iniFile.WriteValue("Users", "remember", "1");
                                iniFile.WriteValue("Users", "name", strUserName);
                                iniFile.WriteValue("Users", "password", strPassword);
                                iniFile.WriteValue("Users", "logintime", DateTime.Now.ToString());
                            }
                            else
                            {
                                iniFile.WriteValue("Users", "remember", "0");
                                iniFile.WriteValue("Users", "name", "");
                                iniFile.WriteValue("Users", "password", "");
                            }

                            //登陆成功
                            MainFrm openWin = new MainFrm();
                            this.Hide();
                            openWin.Show();
                        }
                        else
                        {
                            //密码错误
                            MessageBox.Show("您输入的密码不正确！");
                            txtPassword.Focus();
                        }
                    }
                    else
                    {
                        //用户名不存在
                        MessageBox.Show("您输入的账号不存在！");
                        txtUserName.Focus();
                    }
                    dr.Dispose();
                    cn.Close();
                }
            }
            catch (Exception ex)
            {
                NewsSpider.Utility.Log.SaveErrorLog("用户登录 -> " + ex.Message);
            }
        }

        private void llblReg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.cloudin.so/account/register.aspx?type=cloudin-keywords-dig");
        }

        private void llblWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.cloudin.so/?from=cloudin-keywords-dig");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.cloudin.so/?from=cloudin-keywords-dig");
        }

        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            CheckSoftVersion();
        }

         /// <summary>
        /// 检查软件是否有更新
        /// </summary>
        private void CheckSoftVersion()
        {
            try
            {
                string strNewVersion = NewsSpider.Utility.SoftUpdate.GetNewVersion();
                string strLocationVersion = iniFile2.GetString("cloudin_newsspider", "version", "");

                if (string.IsNullOrEmpty(strNewVersion))
                {
                    strLocationVersion = strNewVersion;
                }

                if (strNewVersion != strLocationVersion)
                {
                    timerUpdate.Enabled = false;

                    if (MessageBox.Show("检测到新版本，您是否需要更新？", "提醒", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        this.Hide();
                        UpdateFrm openWin = new UpdateFrm();
                        openWin.Show();
                    }
                }
                else
                {
                    string strGetVersion = iniFile2.GetString("cloudin_newsspider", "version", "");
                    this.Text = "云里新闻采集大师 " + strGetVersion + "(已是最新版)";
                }

                btnLogin.Enabled = true;
            }
            catch (Exception ex)
            {
                NewsSpider.Utility.Log.SaveErrorLog("检查软件是否有更新 -> " + ex.Message);
            }

        }

    }
}
